package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.StgyException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.AttrKey;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.model.entity.AttrValue;
import com.atguigu.lease.web.admin.service.AttrValueService;
import com.atguigu.lease.web.admin.mapper.AttrValueMapper;
import org.springframework.stereotype.Service;

/**
 * @author gqs
 * @description 针对表【attr_value(房间基本属性值表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class AttrValueServiceImpl extends ServiceImpl<AttrValueMapper, AttrValue>
    implements AttrValueService {

  @Override
  public boolean saveOrUpdateAttrValue(AttrValue attrValue) {
    Long id = attrValue.getId() == null ? -1 : attrValue.getId();
    AttrValue dbAttrValue =
        this.getOne(
            Wrappers.lambdaQuery(AttrValue.class)
                .eq(AttrValue::getName, attrValue.getName())
                .last("limit 1"));
    if (dbAttrValue == null || dbAttrValue.getId().equals(id)) {
      return this.saveOrUpdate(attrValue);
    }
    throw new StgyException(ResultCodeEnum.ATTR_VALUE_EXISTS_ERROR);
  }
}
